#include "head.h"
#define NAME_MAX2 32
#define MSG_MAX 256
#define ADDR_MAX 256
#define TEXT_MAX 2048
typedef struct staffinfo
{
	int usertype;
	char name[NAME_MAX2];
	int age;
	int staffnum;
	char addr[ADDR_MAX];
	char passwd[ADDR_MAX];
	char phone[ADDR_MAX];
	char work[ADDR_MAX];
	char date[ADDR_MAX];
	int level;
	float salary;
}stinfo;

typedef struct messager
{
	char type;
	char text[TEXT_MAX];
	stinfo info;
}MSG;

//**********************       函数声明   ******************************
char buf_g1[NAME_MAX2] = "";
char buf_g2[ADDR_MAX] = "";
void menu1();
void menu2();
void menu2_1();
void menu2_2();
void menu3();
void menu3_2();
void func_serchname(int sockfd);
int func_mode_1(int sockfd);
void func2_mode(int sockfd);
void func2_mode2_1(int sockfd);	
int func2_mode2_3(int sockfd);
void func_searchall(int sockfd);
int recvmodifysuc(int sockfd);

//**************************************************************
int main(int argc, const char *argv[])
{
	int sockfd;
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0)
	{
		perror("sockfd err.");
		return -1;
	}

	struct sockaddr_in serveraddr;
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr = inet_addr(argv[1]);

	socklen_t len = sizeof(serveraddr);

	if(connect(sockfd,(struct sockaddr*)&serveraddr,len)<0)
	{
		perror("connect err.");
		return -1;
	}
	printf("员工管理系统链接成功!\n");
	char buf[128];
	int mode;
	//子进程发送
	while(1)
	{
input:
		menu1();
		printf("请输入您的选择:(数字) >>\n");
		scanf("%d",&mode);
		switch(mode)
		{
			//***************** 管理员模式  ************************
		case 1:
			{
				MSG msg5;
				func_mode_1(sockfd);
				recv(sockfd,&msg5,sizeof(MSG),0);
				if(msg5.info.age == 0)
				{
					printf("登录成功!\n");
					//进入二级菜单
					int mode2;
					while(1)
					{
						menu2();
						printf("请输入命令:(数字) >>");
						scanf("%d",&mode2);
						getchar();
						switch(mode2)
						{
						case 1: //二级菜单管理员查询
							{
								int mode2_1;
								while(1)
								{
									menu2_1();
									printf("请输入查询命令:>");
									scanf("%d",&mode2_1);
									getchar();
									switch(mode2_1)
									{
									case 1://按名字查询
										func_serchname(sockfd);
										break;
									case 2://查询所有
										func_searchall(sockfd);
										break;
									case 3://退出
										break;
									default:
										printf("输入错误！请输入1/2/3\n");
										break;
									}
									if(mode2_1 == 3)
										break;
								}
								break;
							}
						case 2:  //管理员修改
							{
								int m_num;
								int val5;
								int mode2_m;
								MSG msg_m;
								msg_m.type = 'M';
								MSG msg_m_send;
								MSG msg_m_recv;
								msg_m_send.type = 'M';
								strcpy(msg_m.info.name,buf_g1);
								strcpy(msg_m.info.passwd,buf_g2);
								while(1)
								{
									menu2_2();
									printf("请输入你要修改员工的工号:>");
									scanf("%d",&m_num);
									msg_m_send.info.staffnum = m_num;
									send(sockfd,&msg_m_send,sizeof(MSG),0);
									recv(sockfd,&msg_m_recv,sizeof(MSG),0);
									if(msg_m_recv.info.age == 2)
									{
										printf("差无此人，请确认工号输入正确!\n");
										break;
									}
									else if(msg_m_recv.info.age == 3)
									{
										printf("查有此人!\n");
										printf("请输出你要修改的信息:>");
										msg_m.info.staffnum = m_num;
										scanf("%d",&mode2_m);
										while(getchar()!= '\n');
										switch(mode2_m)
										{
										case 1:  //管理员修改姓名
											{
												char buf56[128] = "";
												printf("请输入你要修改的姓名:");
												fgets(msg_m.info.name,NAME_MAX2,stdin);
												if(msg_m.info.name[strlen(msg_m.info.name)-1] == '\n')
													msg_m.info.name[strlen(msg_m.info.name)-1] = '\0';
												strcpy(msg_m.text,"aaa");
												strcpy(msg_m.info.addr,buf_g1); //addr 用来存放管理员的用户名用来插入历史记录
												send(sockfd,&msg_m,sizeof(MSG),0);
												recvmodifysuc(sockfd);	
												break;
											}
										case 2: //管理员修改权限
											printf("修改为管理员/普通员工(1/0):");
m_2_input:
											scanf("%d",&val5);
											if(val5 == 1)
											{
												msg_m.info.usertype = 1;
											}
											else if(val5 == 0)
											{
												msg_m.info.usertype = 0;
											}
											else
											{
												printf("输入错误！请输入1/0：");
												goto m_2_input;
											}
											strcpy(msg_m.text,"bbb");
											send(sockfd,&msg_m,sizeof(MSG),0);
											recvmodifysuc(sockfd);	
											break;
										case 3: //管理员修改电话
											printf("请输入修改后的电话号码：");
											fgets(msg_m.info.phone,ADDR_MAX,stdin);
											if(msg_m.info.phone[strlen(msg_m.info.phone)-1] == '\n')
												msg_m.info.phone[strlen(msg_m.info.phone)-1] = '\0';
											strcpy(msg_m.text,"ccc");
											send(sockfd,&msg_m,sizeof(MSG),0);
											recvmodifysuc(sockfd);	
											break;
										case 4: //管理员修改地址
											printf("请输入修改后的地址:>");
											fgets(msg_m.info.addr,ADDR_MAX,stdin);
											if(msg_m.info.addr[strlen(msg_m.info.addr)-1] == '\n')
												msg_m.info.addr[strlen(msg_m.info.addr)-1] = '\0';
											strcpy(msg_m.text,"ddd");
											send(sockfd,&msg_m,sizeof(MSG),0);
											recvmodifysuc(sockfd);	
											break;
										case 5:  //管理员修改年龄
											printf("请输入修改后的年龄:>");
											scanf("%d",&msg_m.info.age);
											strcpy(msg_m.text,"eee");
											send(sockfd,&msg_m,sizeof(MSG),0);
											recvmodifysuc(sockfd);	
											break;
										case 6:  //管理员修改密码
											printf("请输入修改后的密码:>");
											fgets(msg_m.info.passwd,ADDR_MAX,stdin);
											if(msg_m.info.passwd[strlen(msg_m.info.passwd)-1]=='\n')
												msg_m.info.passwd[strlen(msg_m.info.passwd)-1] = '\0';
											strcpy(msg_m.text,"fff");
											strcpy(msg_m.info.addr,buf_g2); //ａｄｄｒ用来存放管理员的密码用来插入历史记录
											send(sockfd,&msg_m,sizeof(MSG),0);
											recvmodifysuc(sockfd);	

											break;
										case 7: //管理员修改职位
											printf("请输入修改后的职位:>");
											fgets(msg_m.info.work,ADDR_MAX,stdin);
											if(msg_m.info.work[strlen(msg_m.info.work)-1] == '\n')
												msg_m.info.work[strlen(msg_m.info.work)-1] = '\0';
											strcpy(msg_m.text,"ggg");
											send(sockfd,&msg_m,sizeof(MSG),0);
											recvmodifysuc(sockfd);	
											break;
										case 8:  //管理员修改薪资
											printf("请输入修改后的薪资:>");
											scanf("%f",&msg_m.info.salary);
											strcpy(msg_m.text,"hhh");
											send(sockfd,&msg_m,sizeof(MSG),0);
											recvmodifysuc(sockfd);	
											break;
										case 9:  //管理员修改职级
											printf("请输入修改后的职级:>");
											scanf("%d",&msg_m.info.level);
											strcpy(msg_m.text,"iii");
											send(sockfd,&msg_m,sizeof(MSG),0);
											recvmodifysuc(sockfd);	
											break;
										case 10:  //管理员退出
											break;
										default:
											printf("输入错误,请重新输入\n");
											break;
										}
										if(mode2_m == 10)
											break;
									}
								}
								break;
							}
						case 3:  //管理员添加
							{
								MSG msg_rr;
								func2_mode2_3(sockfd);
								recv(sockfd,&msg_rr,sizeof(MSG),0);
								if(msg_rr.info.age == 1)
								{
									printf("添加成功!\n");
								}
								else{
									printf("添加失败!\n");
								}
								break;
							}
						case 4://管理员删除
							{
								MSG msg_d;
								MSG msg_d_rec;
								msg_d.type = 'D';
								strcpy(msg_d.text,buf_g1);
								printf("请输入要删除的用户工号:>");
								scanf("%d",&msg_d.info.staffnum);
								while(getchar()!='\n');
								printf("请输入要删除的用户姓名:>");
								fgets(msg_d.info.name,NAME_MAX2,stdin);
								if(msg_d.info.name[strlen(msg_d.info.name)-1] == '\n')
									msg_d.info.name[strlen(msg_d.info.name)-1] = '\0';
								send(sockfd,&msg_d,sizeof(MSG),0);
								recv(sockfd,&msg_d_rec,sizeof(MSG),0);
								if(msg_d_rec.info.age == 1)
									printf("删除成功！\n");
								else if(msg_d_rec.info.age == 2)
									printf("删除失败,请确认工号和姓名是否一致!\n");

								break;
							}
						case 5://管理员历史
							{
								MSG msg_h;
								msg_h.type = 'h';
								strcpy(msg_h.info.name,buf_g1);
								send(sockfd,&msg_h,sizeof(MSG),0);
								char buf[256] = "";
								while(1)
								{
									recv(sockfd,buf,256,0);
									if(strncmp(buf,"!!!",3)==0)
										break;
									printf("%s\n",buf);
								}
								break;
							}
						case 6: //管理员退出

							break;
						default:
							printf("输入错误!请重新输入命令\n");
							break;
						}
						if(mode2 == 6)
							break;
					}
				}
				else if(msg5.info.age == 1)
				{
					printf("登录失败!\n");
					goto input;
				}
				break;
			}
			//****************************    普通用户  ************************************
		case 2:
			{

				MSG msg_normal;
				MSG msg_n_rec;
				msg_normal.type = 'l';
				int mode3_3;
				while(getchar()!= '\n');
				printf("请输入您的用户名>>");
				fgets(msg_normal.info.name,NAME_MAX2,stdin);
				if(msg_normal.info.name[strlen(msg_normal.info.name)-1] == '\n')
					msg_normal.info.name[strlen(msg_normal.info.name)-1] = '\0';
				printf("请输入您的密码>>");
				fgets(msg_normal.info.passwd,ADDR_MAX,stdin);
				if(msg_normal.info.passwd[strlen(msg_normal.info.passwd)-1] == '\n')
					msg_normal.info.passwd[strlen(msg_normal.info.passwd)-1] = '\0';
				send(sockfd,&msg_normal,sizeof(MSG),0);
				printf("正在发送\n");
				recv(sockfd,&msg_n_rec,sizeof(MSG),0);
				if(msg_n_rec.info.age == 2)
				{
					printf("登录失败!\n");
					goto input;
				}
				else if(msg_n_rec.info.age == 3)
				{

					while(1)
					{
						char normalname[NAME_MAX2];
						strcpy(normalname,msg_normal.info.name);
						menu3();
						printf("请输入命令>>");
						scanf("%d",&mode3_3);
						getchar();
						switch(mode3_3)
						{
						case 1: //普通用户查询
							{
								MSG msg_ns;
								MSG msg_nsrec;
								msg_ns.type = 's';
								strcpy(msg_ns.info.name,normalname);
								send(sockfd,&msg_ns,sizeof(MSG),0);
								char buf[ADDR_MAX] = "";
								fflush(stdout);
								int u = 0;
								while(1)
								{
									recv(sockfd,buf,ADDR_MAX,0);
									if(strncmp(buf,"###",3) == 0)
										break;
									printf("%s |",buf);
								}
								printf("\n查询成功!\n");
								break;
							}
						case 2: //普通用户修改
							{
								int mode3_2;
								MSG msg_nmodify;
								msg_nmodify.type = 'i';
								strcpy(msg_nmodify.info.name,normalname);
								MSG msg_nmrec;
								while(1)
								{
									menu3_2();
									printf("请输入您要修改的选项>>");
									scanf("%d",&mode3_2);
									while(getchar()!='\n');
									switch(mode3_2)
									{
									case 1:   //普通用户修改地址
										{
											printf("请输入你要修改的地址>>");
											fgets(msg_nmodify.info.addr,ADDR_MAX,stdin);
											if(msg_nmodify.info.addr[strlen(msg_nmodify.info.addr)-1]== '\n')
												msg_nmodify.info.addr[strlen(msg_nmodify.info.addr)-1] = '\0';
											strcpy(msg_nmodify.text,"aaa");
											send(sockfd,&msg_nmodify,sizeof(MSG),0);
											recv(sockfd,&msg_nmrec,sizeof(MSG),0);
											if(msg_nmrec.info.age == 4)
											{
												printf("修改成功！\n");
											}
											else
											{
												printf("修改失败!\n");
											}
											break;
										}
									case 2:   //普通用户修改电话
										{
											printf("请输入你要修改的电话>>");
											fgets(msg_nmodify.info.phone,ADDR_MAX,stdin);
											if(msg_nmodify.info.phone[strlen(msg_nmodify.info.phone)-1]== '\n')
												msg_nmodify.info.phone[strlen(msg_nmodify.info.phone)-1] = '\0';
											strcpy(msg_nmodify.text,"bbb");
											send(sockfd,&msg_nmodify,sizeof(MSG),0);
											recv(sockfd,&msg_nmrec,sizeof(MSG),0);
											if(msg_nmrec.info.age == 4)
											{
												printf("修改成功！\n");
											}
											else
											{
												printf("修改失败!\n");
											}
											break;
										}
									case 3:   //普通用户修改密码
										{
											printf("请输入你要修改的密码>>");
											fgets(msg_nmodify.info.passwd,ADDR_MAX,stdin);
											if(msg_nmodify.info.passwd[strlen(msg_nmodify.info.passwd)-1]== '\n')
												msg_nmodify.info.passwd[strlen(msg_nmodify.info.passwd)-1] = '\0';
											strcpy(msg_nmodify.text,"ccc");
											send(sockfd,&msg_nmodify,sizeof(MSG),0);
											recv(sockfd,&msg_nmrec,sizeof(MSG),0);
											if(msg_nmrec.info.age == 4)
											{
												printf("修改成功！\n");
											}
											else
											{
												printf("修改失败!\n");
											}
											break;
										}
									case 4:   //普通用户退出
										break;
									default:
										break;
									}
									if(mode3_2 == 4)
										break;
								}
								break;
							}
						case 3: //普通用户退出
							break;
						default:
							break;
						}
						if(mode3_3 == 3)
							break;
					}
				}
				break;
			}
		case 3:
			goto quit1;
			break;
		default:
			printf("输入错误，请重新输入!\n");
			break;
		}
	}
quit1:
	printf("**************   再见   ****************\n");
	close(sockfd);
	return 0;
}
//**************************一级菜单*******************************
void menu1()
{
	printf("************************************************\n");
	printf("*****  1:管理员模式   2：普通用户 3：退出  *****\n");
	printf("************************************************\n");
}
//************************普通用户二级菜单************************
void menu3()
{
	printf("************************************************\n");
	printf("*****      1.查询    2.修改    3.退出     ******\n");
	printf("************************************************\n");
}
//*************************管理员二级菜单**************************
void menu2()
{
	printf("**************************************************\n");
	printf("* 1:查询 2：修改 3：添加用户 4：删除用户 5：历史 *\n");
	printf("* 6:退出                                          \n");
	printf("**************************************************\n");
}

void menu2_2()
{
	printf("*************************************************\n");
	printf("*                  修改选项                     *\n");
	printf("*************************************************\n");
	printf("*************************************************\n");
	printf("*   1.姓名 2.权限 3.电话 4.地址 5.年龄 6.密码   *\n");
	printf("*   7.职位 8.薪资 9.职级 10.退出                *\n");
	printf("*************************************************\n");
	printf("*************************************************\n");
	printf("*************************************************\n");
}

//************************管理员登录函数***************************
int func_mode_1(int sockfd)
{
	MSG msg;
	msg.type = 'L'; // 消息类型为登录类型
	printf("请输入用户名：");
	scanf("%s",buf_g1);
	printf("请输入密码 :");
	scanf("%s",buf_g2);
	strcpy(msg.info.name,buf_g1);
	strcpy(msg.info.passwd,buf_g2);
	send(sockfd,&msg,sizeof(MSG),0);
}
//***********************管理员添加函数***************************
int func2_mode2_3(int sockfd)
{
	MSG msg_r;
	msg_r.type = 'R';
	strcpy(msg_r.text,buf_g1);
	printf("**********   热烈欢迎新员工！   **************\n");
lab3:
	printf("请输入工号>>");
	scanf("%d",&msg_r.info.staffnum);
	while(getchar() != '\n');
	printf("您输入的工号是: %d\n",msg_r.info.staffnum);
	printf("工号信息一旦录入无法被修改，请确认您输入的是否正确(Y/N)");
	char n,m,k;
lab:
	scanf("%c",&n);
	if(n == 'Y' || n == 'y')
	{

		while(getchar() != '\n');
		printf("请输入用户名：>");
		fgets(msg_r.info.name,NAME_MAX2,stdin);
		if(msg_r.info.name[strlen(msg_r.info.name)-1] == '\n')
			msg_r.info.name[strlen(msg_r.info.name)-1] = '\0';

		printf("请输入用户密码:>");
		fgets(msg_r.info.passwd,ADDR_MAX,stdin);
		if(msg_r.info.passwd[strlen(msg_r.info.passwd)-1] == '\n')
			msg_r.info.passwd[strlen(msg_r.info.passwd)-1] = '\0';
		printf("请输入年龄:>");
		scanf("%d",&msg_r.info.age);
		while(getchar()!='\n');
		printf("请输入电话:>");
		fgets(msg_r.info.phone,ADDR_MAX,stdin);
		if(msg_r.info.phone[strlen(msg_r.info.phone)-1] == '\n')
			msg_r.info.phone[strlen(msg_r.info.phone)-1] = '\0';
		printf("请输入家庭住址:>");
		fgets(msg_r.info.addr,ADDR_MAX,stdin);
		if(msg_r.info.addr[strlen(msg_r.info.addr)-1] == '\n')
			msg_r.info.addr[strlen(msg_r.info.addr)-1] = '\0';
		printf("请输入职位:>");
		fgets(msg_r.info.work,ADDR_MAX,stdin);
		if(msg_r.info.work[strlen(msg_r.info.work)-1] == '\n')
			msg_r.info.work[strlen(msg_r.info.work)-1] = '\0';
		printf("请输入入职日期(格式为0000.00.00):>");
		fgets(msg_r.info.date,ADDR_MAX,stdin);
		if(msg_r.info.date[strlen(msg_r.info.date)-1] == '\n')
			msg_r.info.date[strlen(msg_r.info.date) -1] = '\0';
		printf("请输入职级：");
		scanf("%d",&msg_r.info.level);
		printf("请输入工资:>");
		scanf("%f",&msg_r.info.salary);
		while(getchar() != '\n');
		printf("是否设为管理员(Y/N):");
lab2:
		scanf("%c",&m);
		if(m == 'Y' || m == 'y')
			msg_r.info.usertype = 1;
		else if(m == 'N' || m == 'n')
			msg_r.info.usertype = 0;
		else
		{
			printf("输入错误！请输入Y/N：");
			goto lab2;
		}
		send(sockfd,&msg_r,sizeof(MSG),0);
		while(getchar() != '\n');
		printf("是否继续添加员工(Y/N):");

lab4:
		scanf("%c",&k);
		if(k == 'Y'|| k == 'y')
			goto lab3;
		else if(k == 'N' || k == 'n')
			return 0;
		else
		{
			printf("输入错误，请输入Y/N:");
			goto lab4;
		}
	}
	else if(n == 'n' || n == 'N')
	{
		return 0;
	}
	else
	{
		printf("输入错误,请输入Y/N:>>");
		goto lab;

	}
}
//*******************三级菜单 管理员-1查询菜单****************
void menu2_1()
{
	printf("*****************************************\n");
	printf("*    1.按人名查询  2.全部查询  3.退出   *\n");
	printf("*****************************************\n");	
}
//******************三级菜单 普通用户 -2修改菜单*************
void menu3_2()
{
	printf("*****************************************\n");
	printf("*   1.家庭住址  2.电话  3.密码  4.退出  *\n");
	printf("*****************************************\n");
}
//******************全部查询函数*****************************
void func_searchall(int sockfd)
{
	int i = 0,j;
	char buf[ADDR_MAX] = "";
	MSG msg;
	MSG msg_recv;
	msg.type = 'a';
	strcpy(msg.info.name,buf_g1);
	int n = 0;
	send(sockfd,&msg,sizeof(MSG),0);
	while(1)
	{
		recv(sockfd,buf,ADDR_MAX,0);
		if(strncmp(buf,"###",3) == 0)
			break;
		printf("%s |",buf);
		n++;
		if(n%11 == 0)
			printf("\n");
	}
	printf("查询成功!\n");
}

int recvmodifysuc(int sockfd)
{
	MSG msg_mm;
	recv(sockfd,&msg_mm,sizeof(MSG),0);
	if(msg_mm.info.age == 1)
	{
		printf("修改成功!\n");
	}
	else
	{
		printf("修改失败!\n");
	}
}
//***********  按名字查询客户端函数  ****************
void func_serchname(int sockfd)
{
	MSG msg_c;
	msg_c.type = 'c';
	strcpy(msg_c.text,buf_g1);
	printf("请输入你要查询的姓名>>");
	fgets(msg_c.info.name,NAME_MAX2,stdin);
	if(msg_c.info.name[strlen(msg_c.info.name)-1] == '\n');
	msg_c.info.name[strlen(msg_c.info.name)-1] = '\0';
	char buf[256] = "";
	int n = 0;
	MSG msg_c_recv;
	send(sockfd,&msg_c,sizeof(MSG),0);
	recv(sockfd,&msg_c_recv,sizeof(MSG),0);
	if(msg_c_recv.info.age == 1)
	{
		printf("已从数据库中找到此人\n");
		while(1)
		{
			n++;
			recv(sockfd,buf,256,0);
			if(strncmp(buf,"!!!",3)==0)
				break;
			printf(" %s |",buf);
			if(n % 11 == 0)
				printf("\n");
		}
	}
	else if(msg_c_recv.info.age == 2)
		printf("查无此人\n");
	else
		printf("查询系统错误,请联系开发人员确认!\n");
	printf("\n");
}
